package revenge.jack_cristo.shiyuanbaoyou;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import revenge.jack_cristo.shiyuanbaoyou.adapter.Pinpai_Adapter;
import revenge.jack_cristo.shiyuanbaoyou.bean.PinpaiBean;
import revenge.jack_cristo.shiyuanbaoyou.main_interface.Pinpai_interface;

/**
 * Created by Jack-Cristo on 2016/11/12.
 */

public class Pinpai_frament extends Fragment {
    private static Context context;
    private TabLayout tabLayout;
    private ListView listView;
    private  List<PinpaiBean.RowsBean> list=new ArrayList<>();
    private String baseurl = "http://www.syby8.com/apptools/";
    private Map<String, String> map = new HashMap<>();
    private Pinpai_Adapter adapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    listView.setAdapter(adapter);
                    break;
                case 1:
                    break;


            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pinpai_framelayout, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout = (TabLayout) view.findViewById(R.id.tab_pinpai);
        listView = (ListView) view.findViewById(R.id.pinpai_list);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inittab();
        map.clear();
        //act=brandlist&cpage=1&pagesize=20&bclass=99&v=34
        map.put("act", "brandlist");
        map.put("cpage", "1");
        map.put("pagesize", "20");
        map.put("bclass", "99");
        map.put("v", "34");
        initdata();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                String url="http://www.syby8.com/brand/"+list.get(i).getBrandId()+".html";
                intent.putExtra("weburl",url );
                intent.setClass(context, WebviewActivity.class);
                startActivity(intent);
            }
        });


    }

    private void initdata() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseurl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Pinpai_interface pinpai_interface = retrofit.create(Pinpai_interface.class);

        Call<PinpaiBean> call = pinpai_interface.getpinpailist(map);
        call.enqueue(new Callback<PinpaiBean>() {
            @Override
            public void onResponse(Call<PinpaiBean> call, Response<PinpaiBean> response) {
                if (response.isSuccessful()) {
                   list = response.body().getRows();
                    adapter = new Pinpai_Adapter(context, list);
                    handler.sendEmptyMessage(0);
                }
            }

            @Override
            public void onFailure(Call<PinpaiBean> call, Throwable t) {
            }
        });
    }

    public static Pinpai_frament newInstance(Context con) {
        context = con;

        Bundle args = new Bundle();

        Pinpai_frament fragment = new Pinpai_frament();
        fragment.setArguments(args);
        return fragment;
    }

    private void inittab() {
        TabLayout.Tab tab1 = tabLayout.newTab().setText("最新上线");
        tabLayout.addTab(tab1);
        TabLayout.Tab tab2 = tabLayout.newTab().setText("昨日上线");
        tabLayout.addTab(tab2);
        TabLayout.Tab tab3 = tabLayout.newTab().setText("最后疯狂");
        tabLayout.addTab(tab3);
        TabLayout.Tab tab4 = tabLayout.newTab().setText("精致女装");
        tabLayout.addTab(tab4);
        TabLayout.Tab tab5 = tabLayout.newTab().setText("鞋包配饰");
        tabLayout.addTab(tab5);
        TabLayout.Tab tab6 = tabLayout.newTab().setText("美妆个户");
        tabLayout.addTab(tab6);
        TabLayout.Tab tab7 = tabLayout.newTab().setText("精品男装");
        tabLayout.addTab(tab7);


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        map.clear();
                        map.put("act", "brandlist");
                        map.put("cpage", "1");
                        map.put("pagesize", "20");
                        map.put("bclass", "99");
                        map.put("v", "34");
                        break;
                    case 1:
                        map.clear();
                        map.put("act", "brandlist");
                        map.put("cpage", "1");
                        map.put("pagesize", "20");
                        map.put("bclass", "98");
                        map.put("v", "34");
                        break;
                    case 2:
                        map.clear();
                        map.put("act", "brandlist");
                        map.put("cpage", "1");
                        map.put("pagesize", "20");
                        map.put("bclass", "97");
                        map.put("v", "34");

                        break;
                    case 3:
                        map.clear();
                        map.put("act", "brandlist");
                        map.put("cpage", "1");
                        map.put("pagesize", "20");
                        map.put("bclass", "1");
                        map.put("v", "34");
                        break;
                    case 4:
                        map.clear();
                        map.put("act", "brandlist");
                        map.put("cpage", "1");
                        map.put("pagesize", "20");
                        map.put("bclass", "5");
                        map.put("v", "34");
                        break;
                    case 5:
                        map.clear();
                        map.put("act", "brandlist");
                        map.put("cpage", "1");
                        map.put("pagesize", "20");
                        map.put("bclass", "9");
                        map.put("v", "34");
                        break;
                    case 6:
                        map.clear();
                        map.put("act", "brandlist");
                        map.put("cpage", "1");
                        map.put("pagesize", "20");
                        map.put("bclass", "2");
                        map.put("v", "34");
                        break;
                }
                initdata();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
